/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.OperationWebServiceUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.dao.RwDao;
import com.thinkgem.jeesite.modules.guobo.entity.Location;
import com.thinkgem.jeesite.modules.guobo.entity.Rw;
import com.thinkgem.jeesite.modules.guobo.entity.WarningRole;

/**
 * 读写器Service
 * @author wudavid
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class RwService extends CrudService<RwDao, Rw> {

	public Rw get(String id) {
		return super.get(id);
	}
	
	public List<Rw> findList(Rw rw) {
		return super.findList(rw);
	}
	
	public Page<Rw> findPage(Page<Rw> page, Rw rw) {
		return super.findPage(page, rw);
	}
	
	@Transactional(readOnly = false)
	public void save(Rw rw) {
		/*try {
			//上传没成功或者为新加入的记录时需要上传读写器的位置信息
			if((rw.getDispatchStatus()!=null && rw.getDispatchStatus()!="" 
					&& !"null".equals(rw.getDispatchStatus()) && ("".equals(rw.getDispatchStatus())
					|| "0".equals(rw.getDispatchStatus()))) || StringUtils.isBlank(rw.getId())){
				List<Rw> rws = new ArrayList<Rw>();
				rws.add(rw);
				String result = OperationWebServiceUtils.locationOperation(rws);
				if(result.equals("0")){
					rw.setDispatchStatus("3");
				}
			}			
			super.save(rw);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		if(rw.getId()==null || "".equals(rw.getId()) || "null".equals(rw.getId())){
			rw.setDispatchStatus("0");
			rw.setStatus("0");
		}
		super.save(rw);
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Rw rw) {
		super.delete(rw);
	}
	
	public List<Rw> findListByLocation(String locationId){
		Location loc = new Location();
		loc.setId(locationId);
		Rw rw = new Rw();
		rw.setLocation(loc);
		return dao.findListByLocation(rw);
	}
	
	public Rw getRwByCode(String code){
		Rw rw = new Rw();
		rw.setCode(code);
		return dao.getRwByCode(rw);
	}
	
	/**
	 * 根据读写器ip地址查询读写器信息,ip地址唯一
	 * @param address
	 * @return
	 */
	public Rw getRwByAddress(String address){
		Rw rw = new Rw();
		rw.setAddress(address);
		List<Rw> rws = dao.getRwByAddress(rw);
		if(rws!=null && rws.size()>0){
			return rws.get(0);
		}
		return null;
	}	
	
}